package com.dds.web.controller.union;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.dds.common.utils.ServletUtils;
import com.dds.framework.web.service.TokenService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.dds.common.annotation.Log;
import com.dds.common.core.controller.BaseController;
import com.dds.common.core.domain.AjaxResult;
import com.dds.common.enums.BusinessType;
import com.dds.system.domain.union.UnionShopRecommend;
import com.dds.system.service.union.IUnionShopRecommendService;
import com.dds.common.utils.poi.ExcelUtil;
import com.dds.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletRequest;

/**
 * 联盟商家 APP前端=推荐关系Controller
 *
 * @author gaolianjie
 * @date 2021-04-24
 */
@RestController
@RequestMapping("/union/recommend")
public class UnionShopRecommendController extends BaseController {
    @Autowired
    private IUnionShopRecommendService unionShopRecommendService;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询联盟商家 APP前端=推荐关系列表
     */
    @PreAuthorize("@ss.hasPermi('union:recommend:list')")
    @GetMapping("/list")
    public TableDataInfo list(
            @RequestParam(name = "shopName", required = false) String shopName,
            @RequestParam(name = "startTime", required = false) String startTime,
            @RequestParam(name = "endTime", required = false) String endTime
    ) {
        startPage();

        Map<String, Object> time = new HashMap<>();
        time.put("startTime", startTime);
        time.put("endTime", endTime);

        List<Map<String, Object>> list = unionShopRecommendService.selectUnionShopRecommendList(shopName, time);
        return getDataTable(list);
    }

    /**
     * 获取联盟商家 APP前端=推荐关系详细信息
     */
    @PreAuthorize("@ss.hasPermi('union:recommend:query')")
    @GetMapping(value = "/{shopId}")
    public AjaxResult getInfo(@PathVariable("shopId") String shopId) {
        return AjaxResult.success(unionShopRecommendService.selectUnionShopRecommendById(shopId));
    }

    /**
     * 新增联盟商家 APP前端=推荐关系
     */
    @PreAuthorize("@ss.hasPermi('union:recommend:add')")
    @Log(title = "联盟商家 APP前端=推荐关系", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UnionShopRecommend unionShopRecommend) {
        return toAjax(unionShopRecommendService.insertUnionShopRecommend(
                unionShopRecommend,
                tokenService.getLoginUser(ServletUtils.getRequest()).getUser().getUserId()
        ));
    }

    /**
     * 修改联盟商家 APP前端=推荐关系
     */
    @PreAuthorize("@ss.hasPermi('union:recommend:edit')")
    @Log(title = "联盟商家 APP前端=推荐关系", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UnionShopRecommend unionShopRecommend) {
        return toAjax(unionShopRecommendService.updateUnionShopRecommend(
                unionShopRecommend,
                tokenService.getLoginUser(ServletUtils.getRequest()).getUser().getUserId()
        ));
    }

    /**
     * 删除联盟商家 APP前端=推荐关系
     */
    @PreAuthorize("@ss.hasPermi('union:recommend:remove')")
    @Log(title = "联盟商家 APP前端=推荐关系", businessType = BusinessType.DELETE)
    @DeleteMapping("/{shopIds}")
    public AjaxResult remove(@PathVariable String[] shopIds) {
        return toAjax(unionShopRecommendService.deleteUnionShopRecommendByIds(shopIds));
    }

    @PreAuthorize("@ss.hasPermi('union:recommend:addNew')")
    @Log(title = "店铺推荐", businessType = BusinessType.INSERT)
    @GetMapping("/addNew")
    public AjaxResult addNew() {
        Long userId = tokenService.getLoginUser(ServletUtils.getRequest()).getUser().getUserId();
        return AjaxResult.success(unionShopRecommendService.addNewRecommends(userId));
    }
}
